using System.Collections;
using System.Text.Json;
using AlibabaCloud.SDK.Sts20150401.Models;
using Aliyun.Credentials.Models;
using Aliyun.OSS;

namespace Mathmall.Administration.oss;

public class Oss
{
    // OSS基础信息 替换为实际的 bucket名称、 region-id、host。
    // 域名Endpoint 地域Region
    String bucket = "mathmall";

    String region = "cn-beijing";

    // String host = "oss-cn-beijing.aliyuncs.com";
    string host = "http://mathmall-math.oss-cn-beijing.aliyuncs.com";

    // 限定上传到OSS的文件前缀。
    String upload_dir = "dir";

    //指定过期时间，单位为秒。
    // Long expire_time = 3600L;

    public Dictionary<string, object> GetPostSignatureForOssUpload()
    {
        // AssumeRoleResponseBody.AssumeRoleResponseBodyCredentials sts_data = GetCredential();

        string accessKeyId = "LTAI5tJR1mV6czwwgK1o2A2D";
        string accessKeySecret = "5o4G0tXJmcz5zn3R3zoqj4I33yxhMp";
        string securityToken = "";
        string endpoint = "oss-cn-beijing.aliyuncs.com";

        // 获取x-oss-credential里的date，当前日期，格式为yyyyMMdd
        string date = DateTime.Now.ToString("yyyyMMdd");

        // 生成过期时间 
        string expirationTime = TimeZoneInfo.ConvertTimeToUtc(System.DateTime.Now.AddSeconds(10)).ToString("O");

        // 步骤1：创建policy
        Dictionary<string, object> policy = new Dictionary<string, object>();
        policy.Add("expiration", expirationTime);

        ArrayList conditions = new ArrayList();
        Dictionary<string, string> bucketCondition = new Dictionary<string, string>();
        bucketCondition.Add("bucket", bucket);
        conditions.Add(bucketCondition);

        Dictionary<string, string> signatureVersionnCondition = new Dictionary<string, string>();
        signatureVersionnCondition.Add("x-oss-signature-version", "OSS4-HMAC-SHA256");
        conditions.Add(signatureVersionnCondition);

        Dictionary<string, string> credentialCondition = new Dictionary<string, string>();
        string x_oss_credential = accessKeyId + "/" + date + "/" + region + "/oss/aliyun_v4_request";
        credentialCondition.Add("credential", x_oss_credential);
        conditions.Add(credentialCondition);

        // Dictionary<string, string> securityTokenCondition = new Dictionary<string, string>();
        // securityTokenCondition.Add("securityToken", securityToken);
        // conditions.Add(securityTokenCondition);

        // 获取x-oss-date
        string x_oss_date = TimeZoneInfo.ConvertTimeToUtc(System.DateTime.Now).ToString("yyyyMMddTHHmmssZ");
        Dictionary<string, string> xOssDate = new Dictionary<string, string>();
        xOssDate.Add("x-oss-date", x_oss_date);
        conditions.Add(xOssDate);

        // Dictionary<string, string> dateCondition = new Dictionary<string, string>();
        // dateCondition.Add("date", date);
        // conditions.Add(dateCondition);

        string policyJson = JsonSerializer.Serialize(conditions);
        // 获取签名
        OssClient client = new OssClient(endpoint, accessKeyId, accessKeySecret);
        const string key = "UrlSignature";
        // Step1: Genereates url signature
        var request = new GeneratePresignedUriRequest(bucket, key, SignHttpMethod.Put);
        request.Expiration = DateTime.Now.AddMinutes(5);
        var signature = client.GeneratePresignedUri(request);

        // 生成文件名
        string fileName = date.Substring(0, 4) + "/" + date.Substring(4, 2) + "/" + date.Substring(6, 2) + "/" +
                          Guid.NewGuid();
        // 返回值
        Dictionary<string, object> response = new Dictionary<string, object>();
        response.Add("host", host);
        response.Add("policy", policyJson);
        // response.Add("security_token", securityToken);
        response.Add("x-oss-signature", signature);
        response.Add("x_oss_credential", x_oss_credential);
        response.Add("x_oss_date", x_oss_date);
        response.Add("signatureVersion", "OSS4-HMAC-SHA256");
        response.Add("fileName", fileName);

        return response;
    }

    public static AlibabaCloud.SDK.Sts20150401.Client CreateClient(string accessKeyId, string accessKeySecret)
    {
        var config = new AlibabaCloud.OpenApiClient.Models.Config
        {
            AccessKeyId = accessKeyId,
            AccessKeySecret = accessKeySecret,
            Endpoint = "sts.cn-beijing.aliyuncs.com",
        };
        return new AlibabaCloud.SDK.Sts20150401.Client(config);
    }

    public AssumeRoleResponseBody.AssumeRoleResponseBodyCredentials GetSTSToeknForOssUpload()
    {
        var client = CreateClient("LTAI5tJR1mV6czwwgK1o2A2D", "5o4G0tXJmcz5zn3R3zoqj4I33yxhMp");
        var assumeRoleRequest = new AlibabaCloud.SDK.Sts20150401.Models.AssumeRoleRequest();
        assumeRoleRequest.RoleSessionName = "YOUR_ROLE_SESSION_NAME";

        assumeRoleRequest.RoleArn = "acs:ram::1914656493297464:role/ramoss";
        assumeRoleRequest.DurationSeconds = 3600;
        var runtime = new AlibabaCloud.TeaUtil.Models.RuntimeOptions();
        var response = client.AssumeRoleWithOptions(assumeRoleRequest, runtime);
        var credentials = response.Body.Credentials;

        return credentials;
    }
}